package com.opencloud.ywt.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opencloud.common.model.PageParams;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.mapper.*;
import com.opencloud.ywt.provider.service.BhyfCityService;
import com.opencloud.ywt.provider.service.BhyfWatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 动量服务
 * @author yeyong
 */

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class BhyfWatchServiceImpl implements BhyfWatchService {
    @Autowired
    private BhyfXhbWatchRecordMapper bhyfXhbWatchRecordMapper;

    @Autowired
    private BhyfXhbWatchMapper bhyfXhbWatchMapper;

    @Override
    public IPage<BhyfXhbWatch> findListPage(PageParams pageParams,String startTime,String endTime) {
        BhyfXhbWatch query = pageParams.mapToObject(BhyfXhbWatch.class);
        QueryWrapper<BhyfXhbWatch> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGid()), BhyfXhbWatch::getGid,query.getGid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getWatchtype()), BhyfXhbWatch::getWatchtype,query.getWatchtype());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getEndstatus()), BhyfXhbWatch::getEndstatus,query.getEndstatus());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getClassid()), BhyfXhbWatch::getClassid,query.getClassid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getGradenum()), BhyfXhbWatch::getGradenum,query.getGradenum());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getTeacherid()), BhyfXhbWatch::getTeacherid,query.getTeacherid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getPersonid()), BhyfXhbWatch::getPersonid,query.getPersonid());
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(query.getWatchstatus()), BhyfXhbWatch::getWatchstatus,query.getWatchstatus());
        queryWrapper.lambda().like(ObjectUtils.isNotEmpty(query.getWatchinfo()), BhyfXhbWatch::getWatchinfo,query.getWatchinfo());
        queryWrapper.lambda().between(ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty((endTime)), BhyfXhbWatch::getWatchstart,startTime,endTime);
        queryWrapper.lambda().orderByDesc(BhyfXhbWatch::getId);
        return bhyfXhbWatchMapper.selectPage(new Page(pageParams.getPage(), pageParams.getLimit()), queryWrapper);
    }

    @Override
    public BhyfXhbWatch findInfoById(Integer id){
        return bhyfXhbWatchMapper.selectById(id);
    }

    @Override
    public BhyfXhbWatch addBhyfXhbWatch(BhyfXhbWatch entiy)
    {
         bhyfXhbWatchMapper.insert(entiy);
         return entiy;
    }


    @Override
    public void updateBhyfXhbWatch(BhyfXhbWatch entiy)
    {
        bhyfXhbWatchMapper.updateById(entiy);
    }

    @Override
    public BhyfXhbWatchRecord findRecordInfoById(Integer id){
        return bhyfXhbWatchRecordMapper.selectById(id);
    }

    @Override
    public List<BhyfXhbWatchRecord> findRecordListByWatchId(Integer watchid)
    {
        QueryWrapper<BhyfXhbWatchRecord> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(ObjectUtils.isNotEmpty(watchid), BhyfXhbWatchRecord::getWatchid,watchid);
        queryWrapper.lambda().eq(BhyfXhbWatchRecord::getStatus,1);
        List<BhyfXhbWatchRecord> list = bhyfXhbWatchRecordMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public BhyfXhbWatchRecord addBhyfXhbWatchRecord(BhyfXhbWatchRecord entiy)
    {

        bhyfXhbWatchRecordMapper.insert(entiy);
        return entiy;
    }

    @Override
    public void updateBhyfXhbWatchRecord(BhyfXhbWatchRecord entiy)
    {
        bhyfXhbWatchRecordMapper.updateById(entiy);
    }

}